package com.nl.controller.web.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.nl.controller.web.bean.ExternalMessageTemp;
import com.nl.controller.web.bean.SmsUsers;
import com.nl.controller.web.mapper.ExternalMessageTempMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @desc 外部短信模板同步接口
 * @return null
 * @author ll
 * @date 2020/12/24 10:39
 */
@Service
@Slf4j
public class ExternalMessageTempService {
    public static String NEW_MESSAGE_TEMP = "1";
    public static String UPDATE_MESSAGE_TEMP = "2";
    public static String DELETE_MESSAGE_TEMP = "3";

    @Autowired
    ExternalMessageTempMapper externalMessageTempMapper;
    @Autowired
    private LoginService loginService;

    @Autowired
    private SmsUserService smsUserService;

    public Map<String,Object> externalMessageTemp(String reqJson){

        Map<String, Object> result = new HashMap<>();
        String resultCode="-1";
        String resultDesc="";
        String operateType = "";
        JSONObject jsonObject = JSON.parseObject(reqJson);
        ExternalMessageTemp messageTemp = JSONObject.toJavaObject(jsonObject,ExternalMessageTemp.class);
        operateType = messageTemp.getOperateType();
        //初始化为正常，待审核状态,审核次数为0
        messageTemp.setTempState("1");
        messageTemp.setTempCheckState("0");
        messageTemp.setCheckNumber("0");
        String tempId = messageTemp.getTempId();
        ExternalMessageTemp dbMessageTemp = externalMessageTempMapper.findMessageTempById(tempId);
        //查询地市管理员
        List<SmsUsers> cityUsers =smsUserService.findCityUsersbyCityId(messageTemp.getCityId());
        String externalPlatformName = externalMessageTempMapper.findExternalPlatformName(messageTemp.getPlatformFlag());
        //审核次数
        String checkNumber = "1";
        try {
            if(NEW_MESSAGE_TEMP.equals(operateType)){
                //新建短信模板
                if(dbMessageTemp == null){
                    int insertResult = externalMessageTempMapper.insertMessageTemp(messageTemp);
                    if(insertResult>0){
                        resultCode = "1";
                        resultDesc="新增短信模板成功！";
                        for(SmsUsers cityUser: cityUsers){
                            loginService.sendCode(cityUser.getMsisdn(),"【江苏省应急平台】您好，"+externalPlatformName+"操作员"+messageTemp.getOperatorId()+
                                    "，新增了短信模板，模板编码："+messageTemp.getTempId()+"，模板名称："+messageTemp.getTempName()+"，" +
                                    "模板内容："+messageTemp.getTempContent()+"。审核通过请回复\'Y-"+messageTemp.getTempId()+"+"+checkNumber+"-YJMB\'，审核不通过请回复\'N-"+messageTemp.getTempId()+"+"+checkNumber+"-YJMB\'。");
                        }
                    }else{
                        resultDesc="新增短信模板失败！";
                    }
                }else{
                    resultDesc="【"+tempId+"】"+"模板编码重复！";
                }
            }else if(UPDATE_MESSAGE_TEMP.equals(operateType)){
                //修改短信模板
                if(dbMessageTemp == null){
                    resultDesc="修改失败，"+"【"+tempId+"】"+"暂无该短信模板数据！";
                }else{
                    //审核次数加一
                    checkNumber = Integer.valueOf(messageTemp.getCheckNumber())+1+"";
                    //变更为待审核
                    messageTemp.setTempCheckState("0");
                    int updateResult= externalMessageTempMapper.updateMessageTempById(messageTemp);
                    if(updateResult>0){
                        resultCode = "1";
                        resultDesc="修改短信模板成功！";
                        for(SmsUsers cityUser: cityUsers){
                            loginService.sendCode(cityUser.getMsisdn(),"【江苏省应急平台】您好，"+externalPlatformName+"操作员"+messageTemp.getOperatorId()+
                                    "，修改了短信模板，模板编码："+messageTemp.getTempId()+"，模板名称："+messageTemp.getTempName()+"，" +
                                    "模板内容："+messageTemp.getTempContent()+"。审核通过请回复\'Y-"+messageTemp.getTempId()+"+"+checkNumber+"-YJMB\'，审核不通过请回复\'N-"+messageTemp.getTempId()+"+"+checkNumber+"-YJMB\'。");
                        }
                    }else{
                        resultDesc="修改短信模板失败！";
                    }
                }
            }else if(DELETE_MESSAGE_TEMP.equals(operateType)){
                //删除短信模板
                if(dbMessageTemp == null){
                    resultDesc="删除失败，"+"【"+tempId+"】"+"暂无该短信模板数据！";
                }else{
                    int deleteResult= externalMessageTempMapper.deleteMessageTemp(tempId);
                    if(deleteResult>0){
                        resultCode = "1";
                        resultDesc="删除短信模板成功！";
                    }else{
                        resultDesc="删除短信模板失败！";
                    }
                }
            }
        }catch (Exception e){
            resultDesc="短信模板功能出现异常";
        }
        result.put("resultCode",resultCode);
        result.put("resultDesc",resultDesc);
        return  result ;
    }

    public static void main(String[] args) {
        String reqJson = "{\"platformFlag\":\"1\",\"operateType\":\"1\",\"tempId\":\"1000012\",\"tempName\":\"test\",\"tempContent\":\"hahah\",\"tempType\":\"1\"}";
        JSONObject jsonObject = JSON.parseObject(reqJson);
        ExternalMessageTemp messageTemp = JSONObject.toJavaObject(jsonObject,ExternalMessageTemp.class);
        System.out.println(messageTemp.toString());
    }
}
